/*
 * @Author: fisher
 * @Date: 2024-03-19 22:19:30
 * @LastEditors: fisher luthor-li@qq.com
 * @LastEditTime: 2024-03-19 22:26:29
 * @FilePath: \pscluster\utils\request.js
 * @Description:
 */
const axios = require('axios')
const clusterJson = require('../cluster.json')
const { token, host, port, protocol } = clusterJson
// 创建 axios 实例
const instance = axios.create({
  baseURL: `${protocol}://${host}:${port}`,
  timeout: 20000 // 请求超时时间
})
// 设置请求拦截器
instance.interceptors.request.use(
  config => {
    config.headers['Authorization'] = `Bearer ${token}`
    return config
  },
  err => {
    return Promise.reject(err)
  }
)
// 定义 get 方法
const get = async (url, params = {}, headers = {}) => {
  try {
    const response = await instance.get(url, { params }, { headers })
    return response.data
  } catch (err) {
    console.log(err)
  }
}
// 定义 post 方法
const post = async (url, payload = {}, headers = {}) => {
  try {
    const response = await instance.post(url, payload, { headers })
    return response.data
  } catch (err) {
    console.log(err)
  }
}

module.exports = {
  get,
  post
}
